/*
 * AjaxController.java
 *
 * Created on 15 March 2006, 14:41
 */

package ajax;

import ajax.services.GenericAjaxService;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import web.ApplicationController;
import web.services.ServiceFactory;
import web.util.BusinessFacadeDelegate;
import web.util.BusinessFacadeDelegateFactory;

/**
 *
 * @author Phil
 * @version
 */
public class AjaxController extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    
    public static final String ERROR_REQUEST_NOT_UNDERSTOOD = "E100";
    public static final String ERROR_USER_DOES_NOT_EXIST = "E200";
    public static final String ERROR_USER_ALREADY_IN_MEETING = "E201";

    private static final String SERVICE = "Service";
    public static final String BUSINESS_DELEGATE_REF = ApplicationController.BUSINESS_DELEGATE_REF;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        GenericAjaxService activeService;
        BusinessFacadeDelegate delegate;
        
        ServletContext context = getServletContext();
        HttpSession session = request.getSession();
        String pathInfo = request.getPathInfo();
        PrintWriter out = response.getWriter();
        
        if (null == pathInfo && pathInfo.length() <= 1) {            
            out.print(ERROR_REQUEST_NOT_UNDERSTOOD);
            out.close();
            return;
        }            

        pathInfo = pathInfo.substring(1);
        String[] splitPath = pathInfo.split("/");
        for (int i=0; i < splitPath.length; i++) {
            splitPath[i] = Character.toUpperCase(splitPath[i].charAt(0)) + splitPath[i].substring(1);
        }

        String serviceClass = splitPath[0] + splitPath[1] + SERVICE;
        // String servicePath = splitPath[0];
        // don't need JSPs at the minute, so not implementing that wonderful line.
        
        try {
            delegate = (BusinessFacadeDelegate) session.getAttribute(BUSINESS_DELEGATE_REF);
            
            if (null == delegate) {
                delegate = BusinessFacadeDelegateFactory.getDelegate(null); // no user - not logged in yet!
                session.setAttribute(BUSINESS_DELEGATE_REF, delegate);
            }        

            activeService = ServiceFactory.getInstance().getAjaxService(serviceClass);
            
            activeService.executePreDispatch(request, context);
            activeService.executeDispatch(request, context, out);
            out.close();
            //RequestDispatcher forwarder = request.getRequestDispatcher("/" + servicePath);
            //forwarder.forward(request, response);
            activeService.executePostDispatch(request, context);
        } catch (Exception e) {
            throw new ServletException(e);
        }

    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
